$(document).ready(function() {
	$(".filterTool form select[name='type']").change(function() {
		var selectedIndex = this.selectedIndex;
		var optionValue = new String(this.options[selectedIndex].value).toLowerCase();
		var dormitoryTextInput = $(".filterTool form :text[name='dormitory']");
		if (optionValue == "dormitory") {
			$(".filterTool :submit[name='submit']").attr("disabled", "disabled");
			$(".filterTool .dormitoryInput").show();
			$("#dormitoryName").next().text("");
		} else if (optionValue == "building") {
			$(".filterTool .dormitoryInput").hide();
			$(".filterTool :submit[name='submit']").removeAttr("disabled");
		}
	});
	
	$(".modifyDialog").dialog({
		autoOpen : false,
		modal : true,
		buttons : {
			"取消" : function() {
				$(this).dialog("close");
			},
			"更新" : function() {
				updateLowElectricity(this);
				$(this).dialog("close");
			}
		}
	});
	
	$("#lowElectricityList :checkbox[name='checked']").click(checkedButton);
	
	$("#lowElectricityList :button[name='modify']").click(openModifyDialog);
	//blur同步检查寝室存在性
	$("#dormitory").blur(dormitoryBlur);
	$("#dormitory").focus(dormitoryFocus);
	
	$(":button[name='delete']").click(function() {
		
		var idsAndRows = generateCheckboxIds();
			if (idsAndRows.ids.length == 0) {
				$(".ajaxMessage").text("没有选中任何一个").show();
			} else {
				var confirmed = confirm("你真的要删除吗？");
				if (confirmed) {
					deleteLowElectricity(idsAndRows);
				}
			}
	});
});
function generateCheckboxIds() {
	var ids = new Array();
	var rows = new Array();
	$("#lowElectricityList input:checked").filter("[index]").each(function() {
		ids.push($(this).attr("index"));
		rows.push($(this).parent().parent());
	});
	var idsAndRows = {
		"ids" : ids,
		"rows" : rows
	};
	return idsAndRows;
}
function deleteLowElectricity(idsAndRows) {
	var deleteStatus;
	$.ajax({
		type : "GET",
		data : {"id" : idsAndRows.ids},
		dataType : "json",
		url : "deleteLowElectricity.do",
		success : function(jsonData, textStatus) {
			if (jsonData.status) {
				$(".ajaxMessage").text("删除成功...").show();
				for (var i = 0; i < idsAndRows.rows.length; i++) {
					idsAndRows.rows[i].remove();
				}
			} else {
				$(".ajaxMessage").text("删除失败，原因可能是：" + jsonData.message).show();
			}
		},
		error : function() {
			$(".ajaxMessage").text("请求失败,请稍候再试...");
		}			
	});
}

function openModifyDialog() {
	var modifyDialog = $(".modifyDialog");
	var index = $(this).attr("index");
	var currentRow = $(this).parent().parent();
	modifyDialog.data("modifyRow", currentRow);
	var id = currentRow.find("td[title='id']").text();
	var dormitory = currentRow.find("td[title='dormitory']").text();
	var degree = currentRow.find("td[title='degree']").text();
	var date = currentRow.find("td[title='date']").text();
	
	modifyDialog.find(":hidden[name='id']").val(id);
	modifyDialog.find(":text[name='dormitory']").val(dormitory);
	modifyDialog.find(":text[name='degree']").val(degree);
	modifyDialog.find(":text[name='date']").val(date);
	modifyDialog.dialog("open");		
}

function checkedButton() {
	var checked = $(this).attr("checked");
	$("#lowElectricityList :checkbox").each(function() {
		if (checked) {
			$(this).attr("checked", "checked");
		} else {
			$(this).removeAttr("checked");
		}
	});
}
function updateLowElectricity(modifyDialog) {
	var updateStatus;
	var id = $(modifyDialog).find(":hidden[name='id']").val();
	var dormitory = $(modifyDialog).find(":text[name='dormitory']").val();
	var degree = $(modifyDialog).find(":text[name='degree']").val();
	var date = $(modifyDialog).find(":text[name='date']").val();
	$.ajax({
		async : true,
		type : "GET",
		data : {
			"id" : id,
			"dormitory" : dormitory,
			"degree" : degree,
			"date" : date
		},
		url : "updateLowElectricity.do",
		dataType : "json",
		success : function(jsonData, textStatus) {
			if (jsonData.status) {
				updateStatus = true;
				$(".ajaxMessage").text("更新成功...");
				var modifyRow = $(modifyDialog).data("modifyRow");			
				modifyRow.find("td[title='dormitory']").text(dormitory);
				modifyRow.find("td[title='degree']").text(degree);
				modifyRow.find("td[title='date']").text(date);
				
			} else {
				updateStatus = false;
				$(".ajaxMessage").text("更新失败,原因可能是:" + jsonData.message);
			}
		},
		error : function() {
			$(".ajaxMessage").text("请求失败，请稍候再试...");
		}
	});		
}
function dormitoryBlur() {
	var dormitoryName = $(this).val();
	var buildingId = $("form[name='lowElectricityDTO'] :hidden[name='buildingId']").val();	
	if (dormitoryName != "") {
		var existed = checkDormitoryExist(buildingId, dormitoryName);
		if (!existed) {
			$(this).next().text("该寝室不存在").show();
			$("form[name='lowElectricityDTO'] :submit[name='submit']").attr("disabled", "disabled");
		} else {
			$(".filterForm :submit[name='submit'],form[name='lowElectricityDTO'] :submit[name='submit']").removeAttr("disabled");
		}
	}
	
}
function dormitoryFocus() {
	$(this).next().text("");
}